<div class="easyui-layout" data-options="fit:true">
    <!-- Begin of toolbar -->
    <div id="wu-toolbar-bm">
        <div class="wu-toolbar-button">
            <a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="openAddBm()" plain="true">添加</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="openEditBm()" plain="true">修改</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="removebM()" plain="true">删除</a>
        </div>
    </div>
    <!-- End of toolbar -->
    <table id="wu-datagrid-bm" class="easyui-datagrid" toolbar="#wu-toolbar-bm"></table>
</div>
<!-- Begin of easyui-dialog -->
<div id="wu-dialog-bm" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" style="width:400px; padding:10px;">
    <form id="wu-form-bm" method="post">
        <table>
            <tr>
                <td width="60" align="right">标题:</td>
                <td>
                    <input type="text" name="title" class="wu-text" required="required" />
                </td>
            </tr>
            <tr>
                <td align="right">链接:</td>
                <td>
                    <input type="text" name="url" class="wu-text" required="required" />
                </td>
            </tr>
            <tr>
                <td align="right">排序:</td>
                <td>
                    <input type="text" name="id_desc" class="wu-text" required="required" />
                </td>
            </tr>
            <!-- <tr>
                <td valign="top" align="right">内 容:</td>
                <td>
                    <textarea name="content" rows="6" class="wu-textarea" style="width:260px" required="required"></textarea>
                </td>
            </tr> -->

            <tr>
                <td align="right">字体大小:</td>
                <td>
                    <input type="number" name="size" class="wu-text" required="required" /> px
                </td>
            </tr>

            <tr>
                <td align="right">加粗:</td>
                <td>
                    加粗
                    <input type="radio" name="bold" value="bold"> 正常
                    <input type="radio" name="bold" value="normal">
                </td>
            </tr>

            <tr>
                <td align="right">颜色:</td>
                <td>
                    <input type="text" id="cp1" name="color" />
                </td>
            </tr>

            <input type="text" style="display:none" name="id">
        </table>
    </form>
</div>
<!-- End of easyui-dialog -->
<script type="text/javascript">
    /**
     * Name 添加记录
     */
    function addBm() {
        $('#wu-form-bm').form('submit', {
            url: $.webConfig.apiUrl + 'departmentalink/adddepartmentalink',
            success: function (data) {
                var data = JSON.parse(data);
                if (data['return_code'] == 1) {
                    $.messager.alert('信息提示', '提交成功！', 'info');
                    $('#wu-dialog-bm').dialog('close');
                    $('#wu-datagrid-bm').datagrid('reload');
                } else {
                    $.messager.alert('信息提示', '提交失败！', 'info');
                }
            }
        });
    }

    /**
     * Name 修改记录
     */
    function editBm() {
        $('#wu-form-bm').form('submit', {
            url: $.webConfig.apiUrl + 'departmentalink/updepartmentalink',
            success: function (data) {
                var data = JSON.parse(data);
                //console.log(data);
                if (data) {
                    $.messager.alert('信息提示', '提交成功！', 'info');
                    $('#wu-dialog-bm').dialog('close');
                    $('#wu-datagrid-bm').datagrid('reload');
                } else {
                    $.messager.alert('信息提示', '提交失败！', 'info');
                }
            }
        });
    }

    /**
     * Name 删除记录
     */
    function removebM() {
        var items = $('#wu-datagrid-bm').datagrid('getSelections');
        if (items.length == 0) {
            $.messager.alert('信息提示', '你没选中哦...', 'info');
            return;
        }
        $.messager.confirm('信息提示', '确定要删除该记录？', function (result) {
            if (result) {
                var items = $('#wu-datagrid-bm').datagrid('getSelections');
                var ids = [];
                $(items).each(function () {
                    ids.push(this.id);
                });

                if (ids == 0) {
                    $.messager.alert('信息提示', '你没选中哦...', 'info');
                    return;
                }

                //alert(ids);return;
                $.ajax({
                    url: $.webConfig.apiUrl + 'departmentalink/deldepartmentalink',
                    type: 'post',
                    data: {
                        id: ids[0]
                    },
                    dataType: 'json',
                    success: function (data) {
                        if (data['return_code'] == 1) {
                            $.messager.alert('信息提示', '删除成功！', 'info');
                            $('#wu-datagrid-bm').datagrid('reload');
                        } else {
                            $.messager.alert('信息提示', '删除失败！', 'info');
                        }
                    }
                });
            }
        });
    }

    /**
     * Name 打开添加窗口
     */
    function openAddBm() {
        $('#wu-form-bm').form('clear');
        $('#wu-dialog-bm').dialog({
            closed: false,
            modal: true,
            title: "添加信息",
            cache: false,
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: addBm
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#wu-dialog-bm').dialog('close');
                }
            }]
        });

        $("#cp1").colorpicker({
            fillcolor: true
        });
    }

    /**
     * Name 打开修改窗口
     */
    function openEditBm() {
        $('#wu-form-bm').form('clear');
        var item = $('#wu-datagrid-bm').datagrid('getSelected');

        if (!item) {
            $.messager.alert('信息提示', '你没选中哦...', 'info');
            return;
        }

        $('#wu-form-bm').form('load', item);


        $('#wu-dialog-bm').dialog({
            closed: false,
            modal: true,
            title: "修改信息",
            cache: false,
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: editBm
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#wu-dialog-bm').dialog('close');
                }
            }]
        });

        $("#cp1").colorpicker({
            fillcolor: true
        });
    }

    /**
     * Name 分页过滤器
     */
    function pagerFilter(data) {
        var pageData = {
            total: 18,
            rows: []
        };

        if (data['return_code'] == 1) {
            if (data['total']) {
                pageData['total'] = data['total'];
            }
            pageData['rows'] = data['return_body'];
        }

        return pageData;
    }

    /**
     * Name 载入数据
     */
    $('#wu-datagrid-bm').datagrid({
        url: $.webConfig.apiUrl + 'departmentalink/getdepartmentalink',
        loadFilter: pagerFilter,
        rownumbers: true,
        singleSelect: true,
        pageSize: 20,
        pagination: true,
        multiSort: false,
        fitColumns: true,
        fit: true,
        columns: [
            [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: 'id',
                    width: 100,
                    sortable: true
                },
                {
                    field: 'title',
                    title: '标题',
                    width: 180,
                    sortable: true
                },
                {
                    field: 'url',
                    title: '链接',
                    width: 100
                },
                {
                    field: 'id_desc',
                    title: '排序',
                    width: 100
                },
                // {
                //     field: 'content',
                //     title: '内容',
                //     width: 100
                // },
                {
                    field: 'size',
                    title: '字体大小',
                    width: 100,
                    formatter: function (value, row, index) {
                        if (value) {
                            return value+'px';
                        }
                    }

                },
                {
                    field: 'bold',
                    title: '是否加粗',
                    width: 100,
                    formatter: function (value, row, index) {
                        if (value=='bold') {
                            return '加粗';
                        }else{
                            return '正常'
                        }
                    }
                },
                {
                    field: 'color',
                    title: '字体颜色',
                    width: 100,
                    formatter: function (value, row, index) {
                        if (value) {
                            return '<span style="display:block;width:100%;height:100%;background:'+ value +'">&nbsp;</span>';
                        }
                    }
                }
            ]
        ]
    });
</script>